package com.example.plumneedle.PinLu;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.plumneedle.R;

import java.util.List;

public class AdapterComment1 extends BaseAdapter {

    Context context;
    List<Comment> data;

    public AdapterComment1(Context c, List<Comment> data){
        this.context = c;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, final ViewGroup viewGroup) {
        final ViewHolder holder;
        // 重用convertView
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_comment1, null);
            holder.comment_name = (TextView) convertView.findViewById(R.id.comment_name);
          //  holder.comment_name.setTypeface(Typefaces.get(convertView.getContext(), "Satisfy-Regular.ttf"));
            holder.comment_content = (TextView) convertView.findViewById(R.id.comment_content1);
            holder.comment_liner=convertView.findViewById(R.id.commnt_liner);
           /* final View finalConvertView = convertView;
            holder.comment_liner.setOnClickListener(new View.OnClickListener() {
                // @SuppressLint("LongLogTag")
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(finalConvertView.getContext(), HuiFu.class);
                    intent.putExtra("1",holder.comment_name.getText());
                    Log.i("this","name="+holder.comment_name.getText());
                    Toast.makeText(finalConvertView.getContext(),"n"+holder.comment_name.getText(),Toast.LENGTH_SHORT).show();
                    intent.putExtra("2",holder.comment_content.getText());
                    long time=System.currentTimeMillis();
                    intent.putExtra("3",time);
                    finalConvertView.getContext().startActivity(intent);
                }
            });*/

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        // 适配数据
        holder.comment_name.setText(data.get(i).getName());
        holder.comment_content.setText(data.get(i).getContent());
        return convertView;
    }

    /**
     * 添加一条评论,刷新列表
     * @param comment
     */
    public void addComment(Comment comment){
        data.add(comment);
        notifyDataSetChanged();
    }

    /**
     * 静态类，便于GC回收
     */
    public static class ViewHolder{
        TextView comment_name;
        TextView comment_content;
        LinearLayout comment_liner;
    }
}